package com.yanqu.road.server.manager.systemgroup;

import com.yanqu.road.entity.systemgroup.common.SystemServerGroupWeekData;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Cross2SystemWeekGroupMatchMgr extends TempCrossMgr {

    private static final int MAX_MESSAGE_COUNT = 50;

    //weekId,systemId
    private static Map<Integer, Map<Integer, List<SystemServerGroupWeekData>>> systemGroupMap = new ConcurrentHashMap<>();

    //weekId,systemId groupId
    private static Map<Integer, Map<Integer, Map<Integer, WorldMessageProto.WorldMessageListMsg.Builder>>> chatMsgMap;

    @Override
    public boolean removeExpireData() throws Exception {
        int weekId = GameConfig.getSystemPreWeekId();
        chatMsgMap.remove(weekId);
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        int weekId = GameConfig.getSystemThisWeekId();
        chatMsgMap = new ConcurrentHashMap<>();
        chatMsgMap.put(weekId, new ConcurrentHashMap<>());
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static synchronized void syncMatchGroup(int weekId, int systemId, List<SystemServerGroupWeekData> groupWeekDataList) {
        if(!systemGroupMap.containsKey(weekId)){
            systemGroupMap.put(weekId, new ConcurrentHashMap<>());
        }
        systemGroupMap.get(weekId).put(systemId, groupWeekDataList);
    }

    public static List<SystemServerGroupWeekData> getGroupByWeekId(int weekId, int systemId){
        Map<Integer, List<SystemServerGroupWeekData>> weekMap = systemGroupMap.get(weekId);
        if(weekMap == null){
            return null;
        }
        return weekMap.get(systemId);
    }

    public static SystemServerGroupWeekData getGroupByGroupId(int weekId, int systemId, int groupId){
        List<SystemServerGroupWeekData> list = getGroupByWeekId(weekId, systemId);
        if(list == null){
            return null;
        }
        for (SystemServerGroupWeekData groupWeekData : list) {
            if(groupId == groupWeekData.getGroupId()){
                return groupWeekData;
            }
        }
        return null;
    }

    public static void groupChat(int weekId, int systemId, int groupId, WorldMessageProto.WorldMessageTempMsg chatMsg){
        SystemServerGroupWeekData weekData = getGroupByGroupId(weekId, systemId, groupId);
        if(weekData != null){
            if(!chatMsgMap.containsKey(weekId)){
                synchronized (chatMsgMap){
                    if(!chatMsgMap.containsKey(weekId)){
                        chatMsgMap.put(weekId, new ConcurrentHashMap<>());
                    }
                }
            }
            Map<Integer, Map<Integer, WorldMessageProto.WorldMessageListMsg.Builder>> tempWeekChatMsgMap = chatMsgMap.get(weekId);
            if(!tempWeekChatMsgMap.containsKey(systemId)){
                synchronized (tempWeekChatMsgMap){
                    if(!tempWeekChatMsgMap.containsKey(systemId)){
                        tempWeekChatMsgMap.put(systemId, new ConcurrentHashMap<>());
                    }
                }
            }
            Map<Integer, WorldMessageProto.WorldMessageListMsg.Builder> tempChatMsgMap = tempWeekChatMsgMap.get(systemId);
            if(!tempChatMsgMap.containsKey(groupId)){
                synchronized (tempChatMsgMap){
                    if(!tempChatMsgMap.containsKey(groupId)){
                        tempChatMsgMap.put(groupId, WorldMessageProto.WorldMessageListMsg.newBuilder());
                    }
                }
            }
            WorldMessageProto.WorldMessageListMsg.Builder chatListMsg = tempChatMsgMap.get(groupId);
            synchronized (chatListMsg){
                if(chatListMsg.getDataListCount() >= MAX_MESSAGE_COUNT){
                    chatListMsg.removeDataList(0);
                }
                chatListMsg.addDataList(chatMsg);
            }
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.A_WORLD_MESSAGE_TRANSFER, chatMsg);
            for(long serverId : weekData.getServerIdList()){
                MessageHelper.sendPacket(serverId, 0, pbMsg);
            }
        }
    }

    public static WorldMessageProto.WorldMessageListMsg.Builder getChatListMsg(int weekId, int systemId, int groupId){
        if(!chatMsgMap.containsKey(weekId)){
            return null;
        }
        if(!chatMsgMap.get(weekId).containsKey(systemId)){
            return null;
        }
        return chatMsgMap.get(weekId).get(systemId).get(groupId);
    }
}
